package com.education.module.student;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Service;

import com.education.domain.Student;
import com.education.framework.base.BaseServices;
import com.education.framework.dao.IDao;
import com.education.framework.domain.SearchParams;
import com.education.framework.page.Page;

@Service
public class StudentServices extends BaseServices implements IDao<Student>{

	@Override
	public List<Student> find(SearchParams searchParams, Page page) {
		StringBuffer sql = new StringBuffer();
		sql.append("SELECT id,truename,loginname,passwd,sex,grade_id,professional_id,class_id,remark FROM student");
		
		StringBuffer sqlCase = new StringBuffer();
		String lp = " where ";
		List<Object> argsList = new ArrayList<Object>();
		if(null != searchParams){
			if(null != searchParams.get("title") && !"".equals((String)searchParams.get("title"))){
				sqlCase.append(lp).append(" title like ? ");
				argsList.add("%" + searchParams.get("title") + "%");
				lp = " and ";
			}
		}
		
		sql.append(sqlCase.toString());
		sql.append(" order by truename");
		
		Object[] args = argsList.toArray();
		List<Student> list = dao.query(pageSQL(sql.toString(),page),args,new StudentRowmapper());
		page.setTotalItem(findRecordCount("select count(id) as cNum from student " + sqlCase.toString(),args));
		return list;
	}
	
	public List<Student> find() {
		StringBuffer sql = new StringBuffer();
		sql.append("SELECT id,truename,loginname,passwd,sex,grade_id,professional_id,class_id,remark FROM student");
		
		List<Student> list = dao.query(sql.toString(),new StudentRowmapper());
		return list;
	}

	@Override
	public int save(Student obj) {
		 
 StringBuffer sql = new StringBuffer(); 
 sql.append("insert into student ( "); 
 sql.append("id,truename,sex,grade_id,professional_id "); 
 sql.append(",class_id,remark,loginname,passwd ");  
 sql.append(" ) values(?,?,?,?,?,?,?,?,?) "); 
 Object[] args = {obj.getId(),obj.getTruename(),obj.getSex(),obj.getGradeId(),obj.getProfessionalId(),obj.getClassId() 
 ,obj.getRemark(),obj.getLoginname(),obj.getPasswd() };
		 
		 dao.update(sql.toString(), args);
		return dao.queryForInt("SELECT LAST_INSERT_ID()"); 
	}

	@Override
	public Student findForObject(Integer id) {
		StringBuffer sql = new StringBuffer();
		sql.append("SELECT id,truename,loginname,passwd,sex,grade_id,professional_id,class_id,remark FROM student ");
		sql.append(" where id=? ");
		
		Object[] args = {id};
		return dao.queryForObject(sql.toString(),args,new StudentRowmapper());
	}

	@Override
	public void update(Student obj) {
		 
 StringBuffer sql = new StringBuffer(); 
 sql.append("update student "); 
 sql.append("set  "); 
 sql.append("truename=?,sex=?,grade_id=?,professional_id=?,class_id=? "); 
 sql.append(",remark=? where id=?  "); 
 Object[] args = {obj.getTruename(),obj.getSex(),obj.getGradeId(),obj.getProfessionalId(),obj.getClassId(),obj.getRemark() 
 ,obj.getId() };
		 
		 dao.update(sql.toString(), args);
	}

	@Override
	public void delete(Integer id) {
		String sql = "delete from student where id=?";
		dao.update(sql, new Object[]{id});
	}

	@Override
	public boolean findIsExist(String name) {
		return false;
	}
	
	private class StudentRowmapper implements RowMapper<Student> {
		@Override
		public Student mapRow(ResultSet rs, int arg1) throws SQLException {
			Student obj = new Student();
			obj.setId(rs.getInt("id")); 
obj.setClassId(rs.getInt("class_id")); 
obj.setSex(rs.getString("sex")); 
obj.setRemark(rs.getString("remark")); 
obj.setTruename(rs.getString("truename")); 
obj.setGradeId(rs.getInt("grade_id")); 
obj.setProfessionalId(rs.getInt("professional_id")); 
obj.setLoginname(rs.getString("loginname"));
obj.setPasswd(rs.getString("passwd"));

			return obj;
		}
	}

}
